package com.zfj.service.impl;

import com.zfj.entity.EduCourse;
import com.zfj.entity.EduCourseDescription;
import com.zfj.entity.vo.CourseInfoVo;
import com.zfj.entity.vo.CoursePublishVo;
import com.zfj.handler.GuLiException;
import com.zfj.mapper.EduCourseMapper;
import com.zfj.service.EduCourseDescriptionService;
import com.zfj.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zfj
 * @since 2022-10-07
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程描述注入
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1 向课程表添加课程基本信息
        //CourseInfoVo对象转换eduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert==0){
            //添加失败
            throw new GuLiException(20001,"添加课程信息失败");
        }
        //获取添加课程id
        String cid = eduCourse.getId();

        //2 向课程简介表添加课程简介
        //edu_course_description
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述id就是课程id
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);

        return cid;
    }

    // 根据课程id查询课程信息
    @Override
    public CourseInfoVo getCourseById(String courseId) {
        // 获取课程基本信息
        EduCourse eduCourse = baseMapper.selectById(courseId);
        // 获取课程简介
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);

        // 封装数据
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    // 修改课程信息
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        // 修改课程表数据
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, course);
        int update = baseMapper.updateById(course);
        if (update <= 0){
            throw new GuLiException(20001, "修改课程信息失败");
        }

        // 修改课程简介表数据
        EduCourseDescription courseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, courseDescription);
        boolean update1 = courseDescriptionService.updateById(courseDescription);
        if (!update1){
            throw new GuLiException(20001, "修改课程简介失败");
        }
    }

    //查询确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo courseInfo = baseMapper.getPublishCourseInfo(id);
        return courseInfo;
    }
}
